package reviewAlgorithms.search;

import java.util.ArrayList;
import java.util.List;

public class Subsets {
    public static void main(String[] args) {
        int[] nums = {1,2,3};
        List<List<Integer>> res = subsets(nums);
        for (List<Integer> item : res) {
            System.out.println(item);
        }
    }
    public static List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> resAll = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length < 1) {
            return resAll;
        }
        List<Integer> oneRes = new ArrayList<>();
        for (int i = 0; i <= nums.length; i++) {
            res.clear();
            oneRes.clear();
            int level = 1;
            int index = 0;
            getSubset(nums,i,level,index,res,oneRes);
            resAll.addAll(res);
        }
        return resAll;
    }

    private static void getSubset(int[] nums, int k, int level, int index, List<List<Integer>> res, List<Integer> oneRes) {
        if (level > k){
            res.add(new ArrayList<>(oneRes));
            return;
        }

        for (int i = index; i < nums.length -  (k - level); i++) {
            oneRes.add(nums[i]);
            getSubset(nums,k,level + 1,i + 1,res,oneRes);
            oneRes.remove(oneRes.size() - 1);
        }
    }
}
